package com.yjzx.bean;

import com.yjzx.interfaces.CommonClasses;
import com.yjzx.interfaces.ModifierEnum;
import com.yjzx.util.CollectionUtil;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;

import java.util.List;

/**
 * @author yjzx
 * @date 2023/10/24
 * @description
 */
public class UpdateParam extends JavassistInfo {
    /**
     * 全类型名称
     */
    private String fieldClassName;
    private String paramName;
    private List<String> modifier;
    private List<UpdateAnnotate> annotations;

    private String defaultValue;

    private Boolean enabledGet;
    private Boolean enabledSet;

    public String getFieldClassName() {
        return fieldClassName;
    }

    public void setFieldClassName(String fieldClassName) {
        this.fieldClassName = fieldClassName;
    }

    public String getParamName() {
        return paramName;
    }

    public void setParamName(String paramName) {
        this.paramName = paramName;
    }

    public List<String> getModifier() {
        return modifier;
    }

    public void setModifier(List<String> modifier) {
        this.modifier = modifier;
    }

    public List<UpdateAnnotate> getAnnotations() {
        return annotations;
    }

    public void setAnnotations(List<UpdateAnnotate> annotations) {
        this.annotations = annotations;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public Boolean isEnabledGet() {
        return enabledGet;
    }

    public void setEnabledGet(Boolean enabledGet) {
        this.enabledGet = enabledGet;
    }

    public Boolean isEnabledSet() {
        return enabledSet;
    }

    public void setEnabledSet(Boolean enabledSet) {
        this.enabledSet = enabledSet;
    }

    public CtField toField(ClassPool pool, CtClass ctClass) {
        this.check();
        CtField field = null;
        try {
            field = new CtField(CommonClasses.getCtClass(pool,fieldClassName), paramName, ctClass);
            field.setModifiers(ModifierEnum.modifierToInt(this.modifier));
            if (CollectionUtil.isNotEmpty(this.annotations)){
                for (UpdateAnnotate annotation : this.annotations) {
                    field.getFieldInfo().addAttribute(annotation.toAnnotationAttribute(pool, ctClass));
                }
            }
            defaultValue = defaultValue == null ? null : defaultValue;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return field;
    }

    @Override
    public void check() {
        this.checkParam(this.paramName);
    }
}
